﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class frm_TIN_HOC : Form
    {
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
         public frm_TIN_HOC()
        {
            InitializeComponent();
        }

         private void frm_TIN_HOC_Load(object sender, EventArgs e)
         {
             Load_TH();
         }
         public void Load_TH()
         {
             dataGridView_TIN_HOC.DataSource = sv.getTH();
            
             
         }
         string temp;// biến xóa tạm
         private void dataGridView_TIN_HOC_CellClick(object sender, DataGridViewCellEventArgs e)
         {
             //Hiển thị text lên textbox
             txt_Ma_TH_TH.Text = dataGridView_TIN_HOC.CurrentRow.Cells[1].Value.ToString();
             txt_DienGiai_TH.Text = dataGridView_TIN_HOC.CurrentRow.Cells[0].Value.ToString();
             temp = dataGridView_TIN_HOC.CurrentRow.Cells[1].Value.ToString();//Lấy dòng cần xóa.
             txt_Ma_TH_TH.Enabled = false;
             txt_DienGiai_TH.Enabled = false;
             btn_Luu_TH_TH.Enabled = false;
             btn_Huy_TH.Enabled = false;
         }

         private void btn_Them_TH_TH_Click(object sender, EventArgs e)
         {
             flag = "them";
             btn_Them_TH_TH.Enabled = true;
             btn_Luu_TH_TH.Enabled = true;
             btn_Huy_TH.Enabled = true;
             btn_Xoa_TH.Enabled = false;
             btn_Sua_TH.Enabled = false;
             btn_Thoat_TH.Enabled = true;
             txt_Ma_TH_TH.Enabled = true;
             txt_DienGiai_TH.Enabled = true;
             txt_Ma_TH_TH.Text = "";
             txt_DienGiai_TH.Text = "";
             txt_Ma_TH_TH.Focus();
             
         }

         private void btn_Luu_TH_TH_Click(object sender, EventArgs e)
         {
             ServiceReference1.TIN_HOC th = new ServiceReference1.TIN_HOC();
             string a = txt_Ma_TH_TH.Text;
             string b = txt_DienGiai_TH.Text;
             th.Ma_TH = a;
             th.DienGiai = b;
             if (a == "" | b == "")
             {
                 MessageBox.Show("Xin nhập đầy đủ dữ liệu!");
             }
             else
             {
                 int kq = 0;
                 if (flag == "them")
                 {
                     if (sv.check_math(th))
                         kq = -1;
                     else
                         kq = sv.addTH(th);
                     
                 }
                 else
                 {
                     if (flag == "sua")
                     {
                         sv.setTH(th);
                         kq = 1;
                     }
                 }
                 if (kq != -1)
                 {
                     MessageBox.Show("Hoàn tất!");
                     Load_TH();
                     btn_Sua_TH.Enabled = true;
                     btn_Xoa_TH.Enabled = true;
                 }
                 else
                 {
                     MessageBox.Show("Lỗi!!!");
                 }
             }



         }
         private void btn_Xoa_TH_Click(object sender, EventArgs e)
         {
             ServiceReference1.TIN_HOC th = new ServiceReference1.TIN_HOC();
             th.Ma_TH = temp;
             DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dữ liệu này?", "warnring", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
             if (dr == DialogResult.OK)
             {
                 sv.deleteTH(th);
                 Load_TH();
                 MessageBox.Show("Đã xóa!");
                 txt_Ma_TH_TH.Text = "";
                 txt_DienGiai_TH.Text = "";
             }
         }

         private void btn_Sua_TH_Click(object sender, EventArgs e)
         {
             flag = "sua";
             btn_Luu_TH_TH.Enabled = true;
             btn_Sua_TH.Enabled = false;
             txt_DienGiai_TH.Enabled = true;
             txt_DienGiai_TH.Focus();


         }

         private void btn_Huy_TH_Click(object sender, EventArgs e)
         {
             btn_Luu_TH_TH.Enabled = false;
             btn_Huy_TH.Enabled = false;
             btn_Sua_TH.Enabled = true;
             btn_Xoa_TH.Enabled = true;
             btn_Thoat_TH.Enabled = true;
         }

         private void btn_Thoat_TH_Click(object sender, EventArgs e)
         {
             DialogResult dr = MessageBox.Show("Bạn thật sự muốn thoát bảng này?", "warnring", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
             if (dr == DialogResult.OK)
             {
                 this.Close();
             }
         }
    }
}
